package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.JsBrandMapper;
import com.xinghai.system.domain.JsBrand;
import com.xinghai.system.service.IJsBrandService;

/**
 * 品牌Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-31
 */
@Service
public class JsBrandServiceImpl implements IJsBrandService 
{
    @Autowired
    private JsBrandMapper jsBrandMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询品牌
     * 
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public JsBrand getInfo(Long id)
    {
        return jsBrandMapper.getInfo(id);
    }

    /**
     * 查询品牌列表
     * 
     * @param jsBrand 品牌
     * @return 品牌
     */
    @Override
    public List<JsBrand> list(JsBrand jsBrand)
    {
            jsBrand.setDelFlag("0");
        return jsBrandMapper.list(jsBrand);
    }

    /**
     * 新增品牌
     * 
     * @param jsBrand 品牌
     * @return 结果
     */
    @Override
    public int insert(JsBrand jsBrand)
    {
        jsBrand.setCreateTime(DateUtils.getNowDate());
        return jsBrandMapper.insert(jsBrand);
    }
    /**
     * 批量新增品牌
     *
     * @param jsBrandList 品牌
     * @return 结果
     */
    @Override
    public int insertBatch(List<JsBrand> jsBrandList)
    {
        return jsBrandMapper.insertBatch(jsBrandList);
    }
    /**
     * 修改品牌
     * 
     * @param jsBrand 品牌
     * @return 结果
     */
    @Override
    public int update(JsBrand jsBrand)
    {
        jsBrand.setUpdateTime(DateUtils.getNowDate());
        return jsBrandMapper.update(jsBrand);
    }

    /**
     * 批量删除品牌
     * 
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return jsBrandMapper.deleteBatch(ids);
    }

    /**
     * 删除品牌信息
     * 
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return jsBrandMapper.delete(id);
    }

    /**
     * 导入品牌信息
     *
     * @param list 品牌信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JsBrand> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JsBrandMapper jsBrandMapper = sqlSession.getMapper(JsBrandMapper.class);
            list.stream().forEach(item -> jsBrandMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
